<?php

class MemberFriendAction extends MemberBaseAction
{

    public function _empty()
    {
        $data['id'] = $_SESSION[C('USER_AUTH_KEY')];
        $Member = D("Member")->getByid($data['id']);
        $friends = unserialize($Member['friends']);
        $this->assign('friends', $friends);
        $this->display("MemberFriend:index");
    }

    protected function view($pid)
    {

    }

    public function doadd()
    {
        $data = $_POST;
        $data['id'] = $_SESSION[C('USER_AUTH_KEY')];
        $Member = D("Member")->getByid($data['id']);
        $old = array();
        $old = unserialize($Member['friends']);
        $friend = array();
        $friend[] = $data['friends'];
        $friends = array_merge($friend, $old);
        $data['friends'] = serialize($friends);
        if (D("Member")->save($data)) {
            $this->success("添加成功！");
        } else {
            $this->error("添加失败！");
        }
    }

    public function import()
    {
        $this->display("Member:friendimport");
    }

    public function doimport()
    {
        $data = $_POST;
        //require_once COMMON_PATH .'getfriends/gmail/settings.php';
        //require_once COMMON_PATH .'getfriends/gmail/gmail.php';
        //$res = get_contacts($data['username'], $data['password']);
        //print_r($res);exit;
        include COMMON_PATH . 'getfriends/friendfactory.php';
        $friends = friendFactory::factory(trim($data['username']), trim($data['password']), trim($data['mailtype']));
        print_r($friends);
        exit;
        $this->assign('res', $friends);
        $this->display("Member:friendlist");
    }

    public function doinsertimport()
    {
        $data = $_POST;
        $friend = array();
        foreach ($data['friendlist'] as $key => $value) {
            $friend[] = explode(',', $value);
        }

        $data['id'] = $_SESSION[C('USER_AUTH_KEY')];
        $Member = D("Member")->getByid($data['id']);
        if (!empty($Member['friends'])) {
            $old = array();
            $old = unserialize($Member['friends']);
            $friends = array_merge($old, $friend);
        } else {
            $friends = $friend;
        }
        $data['friends'] = serialize($friend);
        if (D("Member")->save($data)) {
            $this->success("添加成功！");
        } else {
            $this->error("添加失败！");
        }
    }

}

?>